package com.dhh.sky.widget;

import java.util.ArrayList;
import java.util.List;
import com.dhh.sky.R;
import com.dhh.sky.SDAApplication;
import com.dhh.sky.activity.OperationListActivity;
import com.dhh.sky.model.Operation;
import com.dhh.sky.model.SkyDriveObject;
import com.dhh.sky.service.DownloadOperationService;
import com.dhh.sky.service.UploadOperationService;
import com.dhh.sky.util.CUtil;
import com.dhh.sky.util.Constant;
import com.dhh.sky.util.OperationManager;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class OperationListAdapter extends BaseAdapter {
	private OperationListActivity context;
    private LayoutInflater mInflater;
    private List<Operation> ops;
    private OperationManager om;
    private SDAApplication app;
	private NotificationManager notificationManager;
    
    public OperationListAdapter(OperationListActivity context){
    	this.context = context;
    	om = new OperationManager(context);
    	ops = new ArrayList<Operation>();
    	mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	app = (SDAApplication)context.getApplication();
    	notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    }
    
    public void loadOperation(){
    	ops.addAll(om.getOperationReady(Operation.OP_UPLOAD));
    	ops.addAll(om.getOperationReady(Operation.OP_DOWNLOAD));
    }

	@Override
	public int getCount() {
		return ops.size();
	}

	@Override
	public Operation getItem(int position) {
		return ops.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final View view = convertView != null ? convertView : mInflater.inflate(R.layout.operation_list_item,parent,false);
		final Operation op = this.getItem(position);
		ImageView icon = (ImageView) view.findViewById(R.id.oli_icon);
		TextView name = (TextView) view.findViewById(R.id.oli_name);
		TextView status = (TextView) view.findViewById(R.id.oli_status);
		ImageView cancel = (ImageView) view.findViewById(R.id.oli_cancel);
		
		icon.setImageResource(SkyDriveObject.getFileIcon(CUtil.getFileExtesion(op.getMeta())));
		name.setText(op.getMeta());
		
		final Operation nop = om.getOperation(op.getPath(), Integer.valueOf(op.getAction()));		
		if(nop!=null && Integer.valueOf(nop.getAction())==Operation.OP_UPLOAD){
			status.setText(context.getString(R.string.msg_waiting_upload));
		}else if(nop!=null && Integer.valueOf(nop.getAction())==Operation.OP_UPLOAD){
			status.setText(context.getString(R.string.msg_waiting_download));
		}
		
		cancel.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				removeItem(op);
				if(nop!=null && Operation.STATUS_PROCESS==Integer.valueOf(nop.getStatus())){
					if(Integer.valueOf(nop.getAction())==Operation.OP_UPLOAD){
						app.stopUploading();
						notificationManager.cancel(Constant.NOTIFICATION_UPLOAD_ID);
						context.startService(new Intent(context,UploadOperationService.class));
					}else if(Integer.valueOf(nop.getAction())==Operation.OP_DOWNLOAD){
						app.stopDownloading();
						notificationManager.cancel(Constant.NOTIFICATION_DOWNLOAD_ID);
						context.startService(new Intent(context,DownloadOperationService.class));
					}
					
					
				}
				
				om.removeOperation(op.getPath(), Integer.valueOf(op.getAction()));
			}
		});
		
		return view;
	}
	
	public void removeItem(Operation op){
		for(int i=0; i<ops.size(); i++){			
			if(op.getPath().equals(ops.get(i).getPath())){
				ops.remove(i);
				break;
			}
		}
		
		this.notifyDataSetChanged();
	}
}
